package com.ravi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ravi.mapper.CheckRoomMapper;
import com.ravi.mapper.RoomTypeMapper;
import com.ravi.model.dto.CheckRoomDto;
import com.ravi.model.entity.RoomType;
import com.ravi.model.vo.CheckRoomVo;
import com.ravi.service.intf.CheckRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class CheckRoomServiceImpl implements CheckRoomService {

	@Autowired
	private CheckRoomMapper checkRoomMapper;

	@Autowired
	private RoomTypeMapper roomTypeMapper;


	@Override
	public List<CheckRoomVo> getAll(){
		List<CheckRoomDto> dtos = checkRoomMapper.getAllCheckRoomDto();
		return Dtos2Vos(dtos);
	}

	/**
	 * 分页获取全部内容
	 * @param pageNum 当前页码 1-max
	 * @param pageSize 页面展示最大记录树木
	 * @return PageInfo
	 */
	@Override
	public PageInfo<CheckRoomVo> listRoomVoPage(Integer pageNum, Integer pageSize) {
		pageNum = (null == pageNum) || (pageNum <= 0) ? 1 : pageNum;
		PageHelper.startPage(pageNum, pageSize, "room_type_id");
		List<CheckRoomDto> dtos = checkRoomMapper.getAllCheckRoomDto();
		List<CheckRoomVo> vos = Dtos2Vos(dtos);
		return new PageInfo<>(vos);
	}

	/**
	 * 将一个List的诊室dto转换成一个List的诊室vo
	 * @param dtos 诊室dto
	 * @return 诊室vo
	 */
	private List<CheckRoomVo> Dtos2Vos(List<CheckRoomDto> dtos) {
		List<CheckRoomVo> ret = new CopyOnWriteArrayList<>();
		for (CheckRoomDto dto : dtos) {
			String preType = "无";
			if (dto.getPreTypeId() != -1) {
				RoomType type = roomTypeMapper.getRoomTypeById(dto.getPreTypeId());
				preType = type.getRoomType();
			}
			CheckRoomVo vo = new CheckRoomVo(dto, preType);
			ret.add(vo);
		}
		return ret;
	}

}
